package com.foreverwin.jzymes.common.controller;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.domain.CarrierBind;
import com.foreverwin.jzymes.common.service.IActivityRuleAdviceService;
import com.foreverwin.jzymes.common.service.ICarrierBindAdviceService;
import com.foreverwin.jzymes.common.service.ICustomFieldsAdviceService;
import com.foreverwin.jzymes.common.service.IItemAdviceService;
import com.foreverwin.jzymes.common.util.CarrierUtil;
import com.foreverwin.jzymes.common.util.CollectionUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    IActivityRuleAdviceService activityRuleAdviceService;
    @Autowired
    ICarrierBindAdviceService carrierBindAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;

    /**
     * 查询作业规则列表
     *
     * @param activityId
     * @return
     */
    @RequestMapping("/findActivityRule")
    public AjaxResult findActivityRule( @RequestParam String activityId ){
        Map<String,String> rtnRuleMap = activityRuleAdviceService.findActivityRules( activityId );
        return AjaxResult.returnSuccess().put( "value", rtnRuleMap );
    }

    /**
     * 查询本体绑定物料列表
     *
     * @param carrier
     * @return
     */
    @RequestMapping("/findCarrierBindItem")
    public AjaxResult findCarrierBindItem( @RequestParam String carrier ){
        String site = CommonMethods.getSite();
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ) );
        carrier = CarrierUtil.buildCarrier( site, carrier );

        List<CarrierBind> carrierBindList = carrierBindAdviceService.existCarrierBindonBind( site, carrier );
        List<Map<String,Object>> carrierItemList = new ArrayList<>();
        for( CarrierBind carrierBind : carrierBindList ){
            Map<String,Object> rtnItemMap = new HashMap<>();
            Map<String,Object> itemMap = itemAdviceService.existItem( carrierBind.getItemBo() );
            rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
            rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
            rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
            rtnItemMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
            carrierItemList.add( rtnItemMap );
        }

        CollectionUtil.sort( carrierItemList, "description" );
        return AjaxResult.returnSuccess().put( "value", carrierItemList );
    }
}
